<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>远程连接测试</title>
</head>
<script src="jquery/jquery-2.1.0.js"></script>
<script src="jquery/jquery-2.1.0.min.js"></script>

<style>
    .condition {
        display: flex;
        width: 100%;
    }

    .content {
        display: flex;
        width: 100%;
        height: 600px;
    }

    .condition-input {
        width: 30%;
        height: 20px;
        margin-left: 5px;
    }

    .condition-select {
        width: 30%;
        height: 25px;
        margin-left: 5px;
    }

    .condition span {
        width: 20%;
        text-align: right;
    }

    .condition .short-span {
        width: 8%;
    }

    .content-json {
        height: 90%;
        /*height: 87vh;*/
        width: 36%;
        margin: 20px 0 0 20px;
        border-radius: 0;
        resize: none;
        outline: none;
    }

    .content-btn {
        height: 25px;
        margin: auto 1%;
    }
</style>
<body style="height: 100%">
<div>

</div>
<div class="condition">
    <span class="short-span">功能:</span>
    <select class="condition-select" id="changeFn" onchange="changeFn()">
        <option value="0" selected="selected">扫面可访问端口</option>
        <option value="1">phpstudy后门</option>
    </select>
    <span>请求方式:</span><select class="condition-select">
    <option value="GET">GET</option>
    <option value="POST">POST</option>
</select>
    <span class="short-span">ip:</span><input id="ipAddress" class="condition-input" placeholder="ip地址或ip地址+端口号"/>
    <span>访问的方法:</span><input id="request-method" class="condition-input" placeholder="请求的方法"/>
    <span>开始端口:</span><input id="request-minPort" class="condition-input" placeholder="最小端口号,默认80">
    <span>截止端口:</span><input id="request-maxPort" class="condition-input" placeholder="最大端口号,默认65535">
    <button class="content-btn" style="width: 15%" onclick="init()">清空页面</button>
</div>
<div class="content">
    <textarea id="request-content" placeholder="json格式请求参数,例如:
    {
        'name': '老表',
        'sex': '男'
    }" class="content-json" style=""></textarea>
    <button id="testPort" class="content-btn" onclick="getPort()">扫描可访问端口</button>
    <button id="stopScan" disabled onclick="stopScan()" class="content-btn">停止扫描</button>
    <button class="content-btn" onclick="sendMsg()">测试接口</button>
    <textarea id="response-content" placeholder="结果:" readonly class="content-json" style=""></textarea>
</div>
</body>
<script type="text/javascript">

    // 清空页面
    function init() {
        $("#request-method").val('');
        $("#request-minPort").val('');
        $("#request-maxPort").val('');
        $("#ipAddress").val('');
        $("#request-content").val('');
        $("#request-type").val('');
        $("#response-content").val('');
    }

    // 获取可用的端口号
    function getPort() {
        var method = $("#request-method").val();
        var ipAddress = $("#ipAddress").val(); //请求地址
        var minPort = $("#request-minPort").val();// 请求端口号(最小)
        var maxPort = $("#request-maxPort").val(); // 请求最大端口号
        var type = $("#request-type").val(); // 请求最大端口号

        if (!ipAddress || ipAddress == '') {
            alert("请输入请求地址");
            $("#ipAddress").focus;
            return;
        }

        if (!minPort || minPort == '') {
            minPort = 80;
        }

        if (!maxPort || maxPort == '') {
            maxPort = 65535;
        }

        console.log("maxPort:" + maxPort);

        if (!type || type == '') {
            type = 'get';
        }

        if (!method || method == '') {
            method = '';
        }

        // for (var i = minPort; i <= maxPort; i++){
        var data = {
            'method': method,
            'ipAddress': ipAddress,
            'minPort': minPort,
            'maxPort': maxPort,
            'type': type
        };

        $("#response-content").val('');
        $("#response-content").attr('placeholder', '疯狂操作中,请稍后...');
        $("#testPort").html("Drink something...");
        $("#testPort").attr("disabled", true);
        $("#stopScan").attr("disabled", false);
        var start = new Date().getTime();
        $.ajax({
            type: 'POST',
            url: "../laoB/testPorts",
            data: {
                httpCondition: JSON.stringify(data)
            },
            dataType: 'json',
            success: function (res) {
                $("#testPort").html("检查可访问端口");
                $("#testPort").attr("disabled", false);
                $("#stopScan").attr("disabled", true);
                console.log("res:" + res);
                var end = new Date().getTime();
                // 总用时
                var contentTime = (end - start) / 1000;
                $("#response-content").attr('placeholder', '结果:');
                if (res.length > 0) {
                    $("#response-content").val("测试可用端口号结果: " + res + "\n总用时: " + Math.ceil(contentTime) + "s");
                } else {
                    $("#response-content").val("测试可用端口号结果: none \n总用时: " + Math.ceil(contentTime) + "s");
                }
            },
            error: function (res) {
                console.log("res:" + JSON.stringify(res.data));
                $("#response-content").attr('placeholder', '结果:');
                $("#response-content").val("惊现错误,请速与小张联系:" + JSON.stringify(res));
                $("#testPort").html("检查可访问端口");
                $("#testPort").attr("disabled", false);
                $("#stopScan").attr("disabled", true);
            }
        });
    }

    // 测试连接
    function sendMsg() {
        var method = $("#request-method").val();
        var ipAddress = $("#ipAddress").val(); //请求地址
        var type = $("#request-type").val();// 请求端口号(最小)
        var reqContent = $("request-content").val();

        if (!ipAddress || ipAddress == '') {
            alert("请输入请求地址");
            $("#ipAddress").focus;
            return;
        }


        if (!type || type == '') {
            type = 'get';
        }

        if (!method || method == '') {
            method = '';
        }

        if (!reqContent || reqContent == '') {
            reqContent = '';
        }

        var data = {
            'method': method,
            'ipAddress': ipAddress,
            'type': type,
            'reqContent': reqContent
        };

        $("#response-content").val('');
        $("#response-content").attr('placeholder', '疯狂操作中,请稍后...');
        var start = new Date().getTime();
        $.ajax({
            type: 'POST',
            url: "../laoB/sendMsg",
            data: {
                httpCondition: JSON.stringify(data)
            },
            dataType: 'json',
            success: function (res) {
                console.log("res:" + res);
                var end = new Date().getTime();
                // 总用时
                var contentTime = (end - start) / 1000;
                $("#response-content").val("返回数据:" + JSON.stringify(res) + "\n总用时: " + Math.ceil(contentTime) + "s");
            },
            error: function (res) {
                console.log("res:" + JSON.stringify(res.data));
                $("#response-content").val("惊现错误,请速与小张联系:" + JSON.stringify(res));
                $("#response-content").attr('placeholder', '结果:');
            }
        });

    }

    //  停止扫描的方法
    function stopScan() {
        $.ajax({
            type: 'POST',
            url: "../laoB/stopScan",
            dataType: 'json',
            success: function (res) {
                console.log("res:" + res);
                $("#stopScan").attr("disabled", true);
            },
            error: function (res) {
                console.log("res:" + JSON.stringify(res.data));
                $("#stopScan").attr("disabled", true);
            }
        });
    }

    // 切换功能的方法
    function changeFn() {
        var menuInt = $("#changeFn").val();
        console.log(menuInt);
        if (menuInt == 0){
            location.href = "testContent.html";
        }else if (menuInt == 1){
            location.href = "customReq.html";
        }
    }

</script>
</html>